package com.daiqile.myinterest.activity;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.daiqile.myinterest.BaseActivity;
import com.daiqile.myinterest.R;
import com.vondear.rxtools.RxAnimationTool;
import com.vondear.rxtools.RxBarTool;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.l_scrollView)
    ScrollView mScrollView;
    @BindView(R.id.login_logo)
    ImageView loginLogo;
    @BindView(R.id.content)
    LinearLayout content;
    private int screenHeight = 0;//屏幕高度
    private int keyHeight = 0; //软件盘弹起后所占高度
    private float scale = 0.6f; //logo缩放比例
    private int height = 0;

    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        RxBarTool.setTransparentStatusBar(this);//状态栏透明化
        screenHeight = this.getResources().getDisplayMetrics().heightPixels; //获取屏幕高度
        Log.e("screenHeight", String.valueOf(screenHeight));
        keyHeight = screenHeight / 4;//弹起高度为屏幕高度的1/3

        mScrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        mScrollView.addOnLayoutChangeListener(new ViewGroup.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
              /* old是改变前的左上右下坐标点值，没有old的是改变后的左上右下坐标点值*/
                int dist = content.getBottom() - bottom;
                if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
                    Log.e("up-oldBottom", String.valueOf(oldBottom));
                    Log.e("up-getBottom", String.valueOf(content.getBottom()));
                    Log.e("up-bottom", String.valueOf(bottom));
                    Log.e("this", "up------>" + (oldBottom - bottom));
                    Log.e("dist", String.valueOf(dist));
                    if (dist > 0) {
                        //第一个参数为 view对象，第二个参数为 动画改变的类型
                        ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(content, "translationY", 0, -(75 + 16 + dist));
                        mAnimatorTranslateY.setDuration(300);
                        mAnimatorTranslateY.setInterpolator(new LinearInterpolator());
                        mAnimatorTranslateY.start();
                        RxAnimationTool.zoomIn(loginLogo, 0.6f, (75 + 16 + dist));
                    }

                } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {
                    Log.e("this", "down------>" + (bottom - oldBottom));
                    Log.e("down-oldBottom", String.valueOf(oldBottom));
                    Log.e("down-getBottom", String.valueOf(content.getBottom()));
                    Log.e("down-bottom", String.valueOf(bottom));
                    Log.e("dist", String.valueOf(dist));
                    if ((content.getBottom() - oldBottom) > 0) {
                        ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(content, "translationY", content.getTranslationY(), 0);
                        mAnimatorTranslateY.setDuration(300);
                        mAnimatorTranslateY.setInterpolator(new LinearInterpolator());
                        mAnimatorTranslateY.start();
                        //键盘收回后，logo恢复原来大小，位置同样回到初始位置
                        if (dist < 0) {
                            RxAnimationTool.zoomOut(loginLogo, 0.6f);
                        }

                    }
                }
            }
        });
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        startActivity(new Intent(LoginActivity.this,MeActivity.class));
        finish();
    }
}
